package com.event.controller;


import com.alibaba.fastjson.JSON;
import com.event.pojo.StudySrc;
import com.event.service.StudySrcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 学习资料模块 前端控制器
 * </p>
 *
 * @author 孤雏
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/study")
@CrossOrigin(origins = "*",maxAge = 3600)
@Api(value = "学习资料接口",tags = "学习资料接口")
public class StudySrcController {
    @Autowired
    StudySrcService studySrcService;
    @GetMapping("/selectStudySrc")
    @ApiOperation(value = "获取所有学习资料接口,如果传入为空则为查询全部",notes = "传值进来")
    public JSON selectStudySrc(StudySrc studySrc){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        List<StudySrc> studySrcs = studySrcService.selectStudySrc(studySrc);
        System.out.println(studySrcs);
        if(studySrcs.size()>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        map.put("count",studySrcs.size());
        map.put("data",studySrcs);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @GetMapping("/selectStudySrcByName")
    @ApiOperation(value = "按名称模糊查询学习资料",notes = "传值进来")
    public JSON selectStudySrcByName(String srcName){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        List<StudySrc> studySrcs = studySrcService.selectStudySrcByName(srcName);
        System.out.println(studySrcs);
        if(studySrcs.size()>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        map.put("count",studySrcs.size());
        map.put("data",studySrcs);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/deleteStudySrc")
    @ApiOperation(value = "删除学习资料接口",notes = "传值进来")
   public JSON deleteStudySrc(Long srcId){
        Map<Object, Object> map = new ConcurrentHashMap<>();
       String msg;
       boolean b = studySrcService.deleteStudySrc(srcId);
       if(b) msg="200";
       else msg="500";
       map.put("code",0);
       map.put("msg",msg);
       JSON json = (JSON) JSON.toJSON(map);
       return json;
   }
    @PostMapping("/insertStudySrc")
    @ApiOperation(value = "添加学习资料接口",notes = "传值进来")
    public JSON insertStudySrc(StudySrc studySrc){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        int i = studySrcService.insertStudySrc(studySrc);
        if(i>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
    @PostMapping("/updateStudySrc")
    @ApiOperation(value = "更新学习资料接口",notes = "传值进来")
    public JSON updateStudySrc(StudySrc studySrc){
        Map<Object, Object> map = new ConcurrentHashMap<>();
        String msg;
        int i = studySrcService.updateStudySrc(studySrc);
        if(i>0) msg="200";
        else msg="500";
        map.put("code",0);
        map.put("msg",msg);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }
}
